<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" :model="sForm" class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="查询门店">
                  <el-input v-clearEmoji v-model.trim="sForm.storeName" clearable placeholder="请输入" style="width: 95%;"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6" v-if="roleCode !== 'ordinary'">
                <el-form-item label="查询姓名">
                  <el-input v-clearEmoji v-model.trim="sForm.userName" clearable placeholder="请输入" style="width: 95%;"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="98%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeNum" title="门店编号" min-width="100" />
          <vxe-column field="storeName" title="门店名称" min-width="120" />
          <vxe-column field="address" title="门店地址" min-width="200" />
          <vxe-column field="storeUser" title="门店联系人" width="120">
            <template v-slot="{ row }">
              <span>{{ row.storeUser || '-' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="storePhone" title="门店电话" width="120">
            <template v-slot="{ row }">
              <span>{{ row.storePhone || '-' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="higherName" title="负责人" width="120">
            <template v-slot="{ row }">
              <span>{{ row.higherName || '-' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="higherPhone" title="负责人电话" width="120">
            <template v-slot="{ row }">
              <span>{{ row.higherPhone || '-' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="allocationPerson" title="是否分配" width="120">
          </vxe-column>
          <vxe-column fixed="right" min-width="100" title="操作" v-if="roleCode != 'ordinary'">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click="detail(row)">分配门店</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div>
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>

    <el-dialog :visible.sync="dialogTableVisible" title="分配门店">
      <div>
        <el-form ref="addFormRef" :model="addForm" label-width="100px">
          <el-form-item v-if="roleCode != 'manager'" label="选择负责人" style="width: 100%">
            <el-select v-model="addForm.higherIdList" multiple popper-class="eloption" clearable filterable
              placeholder="请选择负责人">
              <el-option v-for="item in leaderList" :key="item.id" :value="item.id" :label="item.employeeName" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择负责人" style="width: 100%" v-if="roleCode == 'manager'">
            <el-select v-model="addForm.higherId" popper-class="eloption" clearable filterable placeholder="请选择负责人">
              <el-option v-for="item in leaderList" :key="item.id" :value="item.id" :label="item.employeeName" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="close" class="celBtn">取 消</el-button>
        <el-button class="addBtn" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getShopList, assignShop, getUserList } from '@/api/store'
export default {
  name: "IndependentAccount",
  data() {
    return {
      dialogTableVisible: false,
      sForm: {
        userName: "",
        storeName: "",
      },
      shopList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      isSearch: false,
      roleCode: '',
      leaderList: [],
      addForm: {
        higherId: '',
        higherIdList: []
      },
      userId: '',
      storeId: ''
    };
  },
  computed: {
    startTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[0];
      } else {
        return "";
      }
    },
    endTime() {
      if (this.date == null) {
        return "";
      }
      if (this.date.length > 0) {
        return this.date[1];
      } else {
        return "";
      }
    },
  },
  created() {
    this.getShopList();
    this.roleCode = this.$store.state.user.roleCodeList[0]
    console.log('store', this.roleCode);
    // this.getAllCount();
  },
  methods: {
    // 查找列表数据
    async getShopList() {
      if (!this.isSearch) {
        this.sForm.storeName = "";
        this.sForm.userName = "";
      }
      const res = await getShopList({
        ...this.page,
        ...this.sForm,
      });
      if (res.code === 200) {
        console.log(res.data, "data");
        this.shopList = res.data.list;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.shopList);
      }
    },
    async getPrincipalList() {
      const res = await getUserList();
      if (res.code === 200) {
        this.leaderList = res.data
      }
    },
    // 搜索方法
    searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getShopList();
    },
    // 重置
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sForm.storeName = "";
      this.sForm.userName = "";
      this.getShopList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      this.getShopList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getShopList();
    },
    close() {
      console.log('');
      this.userId = ''
      this.addForm = {
        higherId: '',
        higherIdList: []
      }
      this.dialogTableVisible = false;
    },
    detail(row) {
      this.getPrincipalList()
      console.log('row', row);
      this.userId = row.id
      this.storeId = row.storeId
      if (this.roleCode == 'manager') {
        this.addForm.higherId = row.higherId
      } else {
        this.addForm.higherIdList = row.allocationIds ? row.allocationIds.split(',') : []
        console.log(' this.addForm.higherIdList ', this.addForm.higherIdList);
      }
      this.dialogTableVisible = true;
    },
    async btnOk() {
      let allocationIds = []
      if (this.roleCode == 'manager') {
        if (this.addForm.higherId) {
          allocationIds = [this.addForm.higherId]
        } else {
          allocationIds = []
        }
      } else {
        allocationIds = [...this.addForm.higherIdList]
      }
      console.log('allocationIds', allocationIds);
      // return 
      const res = await assignShop({
        id: this.userId,
        storeId: this.storeId,
        idList: allocationIds
      });
      if (res.code === 200) {
        this.$message.success("添加成功");
        this.getShopList();
        this.close()
      }
    }
  },
};
</script>

<style scoped lang="scss">
.inner {
  .item {
    p {
      display: flex;

      &:nth-child(1) {
        justify-content: space-between;
      }
    }
  }
}
</style>
